package com.it.park.common.util.file;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;


public class FileUtil {
	
	private static Properties pro = new Properties();
	/**
	 * 计算上传文件的大小
	 * @param resSize
	 * @return
	 */
	
	public static String getFileSize(long resSize ){
		String size = "";
		if(resSize <1000){
			size = resSize+"K";
		}else if(resSize <1000*1000){
			size = (double)(resSize /1000)+"KB";
		}else{
			size= (double) (resSize/1000/1000)+"MB";
		}
		return size;
	} 
	
	/**
	 * 读取配置文件
	 */
	public static void getConfigPath(HttpServletRequest request){
		String path = request.getServletContext().getRealPath("/")+"WEB-INF"+File.separator+"classes"+File.separator+"config.properties";
		try {
			//读取配置文件       
			InputStream is = new FileInputStream(new File(path));
			pro.load(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取配置文件中的上传根目录
	 */
	public static String getUploadPath(HttpServletRequest request){
		getConfigPath(request);
		String uploadPath = pro.getProperty("uploadPath");
		File file = new File(uploadPath);
		if(!file.exists()){//目录不存在创建
			file.mkdir();
		}
		return uploadPath;
	}
	
	/**
	 *创建和获取图片文件路径 
	 */
	public static String getImgPath(HttpServletRequest request,String userName){
		String imgPath = getUploadPath(request);
		File file = new File(imgPath+File.separator+"image"+File.separator+userName);
		if(!file.exists()){//目录不存在创建
			file.mkdirs();
		}
		return file.toString();
	}
	
	/**
	 *创建和获取图书文件路径 
	 */
	public static String getBookFilePath(HttpServletRequest request,String userName){
		String imgPath = getUploadPath(request);
		File file = new File(imgPath+File.separator+"bookfile"+File.separator+userName);
		if(!file.exists()){//目录不存在创建
			file.mkdirs();
		}
		return file.toString();
	}
	
	/**
	 * 存放上传的个人文件资源路径
	 */
	public static String getResourcePath(HttpServletRequest request,String userName){
		String resourcePath = getUploadPath(request);
		File file = new File(resourcePath+File.separator+"resource"+File.separator+userName);
		if(!file.exists()){//目录不存在创建
			file.mkdirs();
		}
		return file.toString();
	}
	
	/**
	 * 存放上传的个人文件资源
	 */
	public static String getResourceFilePath(HttpServletRequest request,String userName,String fileName){
		String resourcePath = getResourcePath(request,userName);
		File file = new File(resourcePath+File.separator+fileName);
		try {
			if(!file.exists()){//目录不存在创建
				file.createNewFile();
			}
		} catch (IOException e) {
				e.printStackTrace();
		}
		return file.toString();
	}
	
	/**
	 * 存个人上传的书籍图片信息
	 */
	public static String getImgFilePath(HttpServletRequest request,String userName,String fileName){
		String imgRootPath = getImgPath(request,userName);
		File file = new File(imgRootPath+File.separator+fileName);
		try {
			if(!file.exists()){//目录不存在创建
				file.createNewFile();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file.toString();
	}
	
	/**
	 * 存个人相关书籍信息
	 */
	public static String getBookFilePath(HttpServletRequest request,String userName,String fileName){
		String bookRootPath = getBookFilePath(request,userName);
		File file = new File(bookRootPath+File.separator+fileName);
		try {
			if(!file.exists()){//目录不存在创建
				file.createNewFile();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file.toString();
	}
	
	/**
	 * 写文件到本地
	 */
	public static void writeFile(String bookPath,InputStream is){
		try {
			FileOutputStream fos = new FileOutputStream(bookPath);
			fos.write(getBytes(is));
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*public static String getUploadPath(HttpServletRequest request,String fileName){
		try {
			String path = request.getServletContext().getRealPath("/")+"WEB-INF"+File.separator+"classes"+File.separator+"config.properties";
			//读取配置文件       
			InputStream is = new FileInputStream(new File(path));
			pro.load(is);
			String configPath = pro.getProperty("uploadPath");
			File file = new File(configPath);
			if(!file.exists()){//目录不存在创建
				file.mkdirs();
			}
			 file = new File(configPath+File.separator+fileName);
			 if(!file.exists()){
				 file.createNewFile();
			 }
			return file.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
	*/
	/**
	 * 根据流读取文件
	 */
	public static  byte[] getBytes(InputStream is){
			byte[] buffer = null;
			 try {
				ByteArrayOutputStream stream = new ByteArrayOutputStream(1000);
				byte[] bytes = new byte[512];
				int len = 0;  
	            while ((len = is.read(bytes)) != -1) {  
	                stream.write(bytes, 0, len);  
	             }  
	            buffer = stream.toByteArray(); 
	             stream.close();  
	             is.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		 return buffer;
	}
	
	/**
	 * 根据路径读取文件
	 */
	public static  byte[] getBytes(String filePath){
		InputStream is = null;
		try {
			is = new FileInputStream(new File(filePath));
			return getBytes(is);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
